package com.xunwulian.cmpp.pdu;

import com.xunwulian.base.util.GsonUtil;
import com.xunwulian.cmpp.sms.ByteBuffer;
import com.xunwulian.cmpp.sms.NotEnoughDataInByteBufferException;
import com.xunwulian.cmpp.sms.PDUException;
import com.xunwulian.cmpp.util.CmppConstant;
import com.google.gson.Gson;

import java.io.Serializable;

public class Query extends Request implements Serializable {

    private static final long serialVersionUID = 7890539155310810301L;
    private String time = "";
    private byte queryType = 0x00;
    private String queryCode = "";
    private String reserve = "";

    public Query() {
        super(CmppConstant.CMD_QUERY);
    }

    protected Response createResponse() {
        return new QueryResp();
    }

    public void setData(ByteBuffer buffer) throws PDUException {
        header.setData(buffer);
        setBody(buffer);
    }

    public ByteBuffer getData() {
        ByteBuffer bodyBuf = getBody();
        header.setCommandLength(CmppConstant.PDU_HEADER_SIZE + bodyBuf.length());
        ByteBuffer buffer = header.getData();
        buffer.appendBuffer(bodyBuf);
        return buffer;
    }

    private ByteBuffer getBody() {
        ByteBuffer buffer = new ByteBuffer();
        buffer.appendString(time, 8);
        buffer.appendByte(queryType);
        buffer.appendString(queryCode, 10);
        buffer.appendString(reserve, 8);
        return buffer;
    }

    public void setBody(ByteBuffer buffer) throws PDUException {
        try {
            time = buffer.removeStringEx(8);
            queryType = buffer.removeByte();
            queryCode = buffer.removeStringEx(10);
            reserve = buffer.removeStringEx(8);
        } catch (NotEnoughDataInByteBufferException e) {
            e.printStackTrace();
            throw new PDUException(e);
        }
    }

    public String getQueryCode() {
        return queryCode;
    }

    public void setQueryCode(String queryCode) {
        this.queryCode = queryCode;
    }

    public int getQueryType() {
        return queryType;
    }

    public void setQueryType(byte queryType) {
        this.queryType = queryType;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getReserve() {
        return reserve;
    }

    public void setReserve(String reserve) {
        this.reserve = reserve;
    }

    public String name() {
        return "CMPP Query";
    }

    public String dump() {
        return GsonUtil.toJson(this, this.getClass());
    }

}
